package com.parking.carsystem.parkingchargesystem.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;

public class AddInvoiceActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.user_invoice)
    TextView userInvoice;
    @BindView(R.id.company_invoice)
    TextView companyInvoice;
    @BindView(R.id.invioce_title)
    EditText invioceTitle;
    @BindView(R.id.invioce_email)
    EditText invioceEmail;
    @BindView(R.id.invioce_Identification)
    EditText invioceIdentification;
    @BindView(R.id.invioce_address)
    EditText invioceAddress;
    @BindView(R.id.invioce_phone)
    EditText inviocePhone;
    @BindView(R.id.invioce_bank)
    EditText invioceBank;
    @BindView(R.id.invioce_bank_account)
    EditText invioceBankAccount;
    @BindView(R.id.invoice_other_message)
    LinearLayout invoiceOtherMessage;
    @BindView(R.id.save)
    TextView save;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_add_invoice;
    }

    @Override
    public void initView() {
        back.setVisibility(View.VISIBLE);
        text.setText("新增发票");
        userInvoice.setOnClickListener(this);
        companyInvoice.setOnClickListener(this);
        save.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_invoice:
                userInvoice.setBackgroundResource(R.drawable.invoice_blue_backgroud);
                userInvoice.setTextColor(getResources().getColor(R.color.white));
                companyInvoice.setTextColor(getResources().getColor(R.color.login_hint));
                companyInvoice.setBackgroundResource(R.drawable.invoice_plate_code_gray_bg);
                invoiceOtherMessage.setVisibility(View.GONE);
                break;
            case R.id.company_invoice:
                userInvoice.setBackgroundResource(R.drawable.invoice_plate_code_gray_bg);
                companyInvoice.setBackgroundResource(R.drawable.invoice_blue_backgroud);
                userInvoice.setTextColor(getResources().getColor(R.color.login_hint));
                companyInvoice.setTextColor(getResources().getColor(R.color.white));
                invoiceOtherMessage.setVisibility(View.VISIBLE);
                break;
            case R.id.save:
                /**
                 * 01:企业
                 * 02：机关事业单位
                 * 03：个人
                 * 04：其它
                 */
                if (invoiceOtherMessage.getVisibility() == View.VISIBLE) {
                    //单位的
                    inVoiceSave("01");
                } else {
                    //个人的
                    inVoiceSave("03");
                }
                break;
        }
    }

    /**
     * 保存发票信息
     *
     * @param type
     */
    private void inVoiceSave(String type) {
        String invioceTitleMsg = invioceTitle.getText().toString();
        String invioceEmailMsg = invioceEmail.getText().toString();
        String invioceIdentificationMsg = invioceIdentification.getText().toString();
        String invioceAddressMsg = invioceAddress.getText().toString();
        String inviocePhoneMsg = inviocePhone.getText().toString();
        String invioceBankMsg = invioceBank.getText().toString();
        String invioceBankAccountMsg = invioceBankAccount.getText().toString();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("type", type);
        if (TextUtils.isEmpty(invioceTitleMsg)) {
            showToast("请填写发票抬头");
            return;
        }
        hashMap.put("name", invioceTitleMsg);
        if (type.equals("01")) {
            hashMap.put("email", invioceEmailMsg);
            hashMap.put("tax", invioceIdentificationMsg);
            hashMap.put("addr", invioceAddressMsg);
            hashMap.put("phone", inviocePhoneMsg);
            hashMap.put("bankName", invioceBankMsg);
            hashMap.put("bankAccount", invioceBankAccountMsg);
        }
        JSONObject jsonObject = new JSONObject(hashMap);
        showLoading();
        OkGo.<String>post(Constant.url + Constant.INVOICEADD)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject1 = new JSONObject(response.body());
                            int code = jsonObject1.getInt("code");
                            String msg = jsonObject1.getString("msg");
                            stopLoading();
                            if(code == 200){
                                setResult(Constant.MONTHLYSUBSCRIPTIONPARKING);
                                finish();
                            }else {
                                showToast(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        stopLoading();
                    }
                });
    }
}
